package com.hzqy.web.ac;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.AreaUtil;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.service.product.ProductServiceBean;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.SysProductVo;

@Controller
@RequestMapping("/")
public class ProductAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	private final String picType = "产品";
	@Resource
	private ProductServiceBean productServiceBean;
	
	@RequestMapping(value = "/product/product_list", params = {"page", "rows"})
	@ResponseBody
	public Object productList(ModelMap map,SysProductVo sysProductVo, int page, int rows) {
		Map<String,Object> productListMap = new HashMap<String, Object>();
		productListMap =  productServiceBean.selectProductList(sysProductVo, page, rows);
		return productListMap;
	}
	
	@RequestMapping("/product/delete_product")
	@ResponseBody
	public Object deleteProduct(int f_sp_id) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = productServiceBean.deleteProduct(f_sp_id);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}
	
	@RequestMapping("/product/insert_product")
	@ResponseBody
	public Object insertproduct(SysProductVo spv,String picJson, String[] companyId) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = productServiceBean.insertProduct(spv,picJson, companyId);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","新增内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","新增内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/product/select_product")
	@ResponseBody
	public Object selectProduct(int f_sp_id) {
		SysProductVo spv = productServiceBean.selectProduct(f_sp_id);
		return spv;
	}
	
	@RequestMapping("/product/update_product")
	@ResponseBody
	public Object updateproduct(SysProductVo spv, String picJson, String[] companyId) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = productServiceBean.updateProduct(spv,picJson,picType,companyId);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","修改内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","修改内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/product/select_product_org")
	@ResponseBody
	public List<PmsOrgVo> selectProductOrg(int f_sp_id) {
		List<PmsOrgVo> orgList = productServiceBean.selectProductOrg(f_sp_id);
		return orgList;
	}
	
	@RequestMapping("/product/select_product_org_list")
	@ResponseBody
	public Object selectProductOrgList(int f_sp_id) {
		List<PmsOrgVo> orgList = productServiceBean.selectProductOrgList(f_sp_id);
		return orgList;
	}
	
	/**
	 * min , max 账号数量区间
	 * minScore,maxScore 平均分区间
	 */
	@RequestMapping("/product/select_product_org_bind")
	@ResponseBody
	public Object selectProductOrgBind(int f_sp_id,String f_po_name, String f_po_address, String f_po_stars, String f_po_price, int min , int max, int bind_status,float minScore , float maxScore,String labelId) {
		List<PmsOrgVo> orgList = productServiceBean.selectProductOrgBind(f_sp_id,f_po_name,f_po_address,f_po_stars,f_po_price,min,max,bind_status,minScore,maxScore,labelId);
		return orgList;
	}
	
	/**
	 * min , max 账号数量区间
	 * minScore,maxScore 平均分区间
	 */
	@RequestMapping("/product/export_product_org_bind")
	@ResponseBody
	public void exportProductOrgBind(int f_sp_id,String f_po_name, String f_po_address, String f_po_stars, String f_po_price, int min , int max, int bind_status,float minScore , float maxScore,String labelId,HttpServletResponse response) {
		List<PmsOrgVo> orgList = productServiceBean.selectProductOrgBind(f_sp_id,f_po_name,f_po_address,f_po_stars,f_po_price,min,max,bind_status,minScore,maxScore,labelId);
		for(int i=0;i<orgList.size();i++){
			orgList.get(i).setF_po_address_detail(AreaUtil.getAreaName(orgList.get(i).getF_po_address()));
		}
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_po_name","组织名称"));
		excelHeader.add(new MyEntry<String,String>("f_po_stars","组织类型"));
		excelHeader.add(new MyEntry<String,String>("user_count","账号数量"));
		excelHeader.add(new MyEntry<String,String>("f_po_price","组织价格"));
		excelHeader.add(new MyEntry<String,String>("f_po_createAt","创建时间"));
		excelHeader.add(new MyEntry<String,String>("f_po_address_detail","所属地区"));
		ExcelUtil.generateExcel(orgList, excelHeader, response);
	}
	
	/**
	 * 批量（解除）绑定
	 */
	@RequestMapping("/product/product_org_bind")
	@ResponseBody
	public Object insertProductOrgBind(int f_sp_id,String ids,int bind_status) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("resultTitle", "绑定结果");
		int flag = productServiceBean.insertProductOrgBind(f_sp_id,ids,bind_status);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","操作成功!");
		}
		return resultMap;
	}
}
